//
//  XYJXCViewController.m
//  徙游记-01
//
//  Created by hcios on 2017/7/10.
//  Copyright © 2017年 鹿林. All rights reserved.
//

#import "XYJXCViewController.h"
#import "XYJXCModel.h"
#import "XYJXCCollectionViewCell.h"
#import "XYJXCResultViewController.h"

@interface XYJXCViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>{
    UICollectionView *xc_collectionView;
    NSMutableArray *_dataArr;
}

@end

static NSString *const CollectionViewCellIdentifier = @"cellID";

@implementation XYJXCViewController

-(instancetype)init{
    if (self = [super init]) {
        _dataArr = [[NSMutableArray alloc] init];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"行程";
    [self netWorking];
    
    [self initCollectionView];
    
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = NO;
    self.tabBarController.tabBar.hidden = YES;
}


-(void)initCollectionView{
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    layout.itemSize = CGSizeMake(SCREEN_WIDTH - 20, 200);
    layout.minimumLineSpacing = 10.0f;
    xc_collectionView = [[UICollectionView alloc] initWithFrame:RECT_MAKE(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64) collectionViewLayout:layout];
    xc_collectionView.delegate = self;
    xc_collectionView.dataSource = self;
    [xc_collectionView registerNib:[UINib nibWithNibName:@"XYJXCCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:CollectionViewCellIdentifier];
    xc_collectionView.backgroundColor = RGB(214, 226, 238);
    [self.view addSubview:xc_collectionView];
}


#pragma mark - CollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return _dataArr.count;
}


- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    XYJXCCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:CollectionViewCellIdentifier forIndexPath:indexPath];
    cell.model = _dataArr[indexPath.item];
    return cell;
}


#pragma mark - CollectionViewDelegate
//设置边距
- (UIEdgeInsets) collectionView:(UICollectionView *)collectionView
                         layout:(UICollectionViewLayout *)collectionViewLayout
         insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(15.0f, 10.0f, 0.0f, 10.0f);
}

//点击方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    NSLog(@"%ld%ld",(long)[indexPath section],(long)[indexPath row]);
    
    XYJXCModel *model = _dataArr[indexPath.row];
    XYJXCResultViewController *VC = [[XYJXCResultViewController alloc] init];
    VC.selfID = model.xc_selfID;
    VC.days_count = model.xc_plan_days_count;
    VC.nodes_count = model.xc_plan_nodes_count;
    [self.navigationController pushViewController:VC animated:YES];
    
}



-(void)netWorking{
    
    NSString *urlString = [NSString stringWithFormat:@"http://chanyouji.com/api/destinations/plans/%@.json",_selfID];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:urlString parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        for (NSDictionary *dict in responseObject) {
            XYJXCModel *model = [[XYJXCModel alloc] initWithDict:dict];
            [_dataArr addObject:model];
        }
        [xc_collectionView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        NSLog(@"网络请求失败");
    }];
}




@end
